package com.xhx.accounting;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;


import java.util.List;
import java.util.Map;

public class dayDetailAdapter extends BaseAdapter {
    private List<Map<String, Object>> data;
    private LayoutInflater layoutInflater;
    private Context context;

    public dayDetailAdapter(Context context, List<Map<String, Object>> data) {
        this.context = context;
        this.data = data;
        this.layoutInflater = LayoutInflater.from(context);
    }

    //组件集合，对应list.xml中的控件
    public final class Zujian {
        public TextView title;
        public TextView amount;
        public ImageView icon;
        //public TextView note;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    /**
     * 获得某一位置的数据
     */
    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    /**
     * 获得唯一标识
     */
    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Zujian zujian;
        if (convertView == null) {
            zujian = new Zujian();
            //获得组件，实例化组件
            convertView = layoutInflater.inflate(R.layout.dailydetail, null);
            zujian.title = convertView.findViewById(R.id.detailName);
            zujian.amount = convertView.findViewById(R.id.detailAmount);
            zujian.icon = convertView.findViewById(R.id.detailIcon);
            //zujian.note = convertView.findViewById(R.id.detailNote);
            convertView.setTag(zujian);
        } else {
            zujian = (Zujian) convertView.getTag();
        }
        String sign;
        if ((int)data.get(position).get("classification_direction") == -1) {
            sign = "-";
        } else {
            sign = "+";
        }
        //绑定数据
        zujian.title.setText((String) data.get(position).get("typeName"));
        zujian.amount.setText(sign+data.get(position).get("amount"));
        zujian.icon.setImageResource(context.getResources().getIdentifier((String)(data.get(position)).get("typeIcon"),"drawable",context.getPackageName()));
        //zujian.note.setText((String) data.get(position).get("notes"));
        return convertView;
    }

}

